//
//  ICMAppointmentActivityView.m
//  ICrEAM
//
//  Created by rainimute on 16/11/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMAppointmentActivityModel.h"
#import "ICMAppointmentActivityView.h"
#import "ICMActivityModel.h"
#import "ICMAppointmentOtherInfoModel.h"

@interface ICMAppointmentActivityView()<UITableViewDelegate,UITableViewDataSource,ICMAppointmentActivityCellDelegate>

@property (nonatomic, strong) NSMutableArray *selectedActivityCodes;

@end

@implementation ICMAppointmentActivityView
#pragma mark    初始化
- (instancetype)initSelecteActivityViewWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.activityTableView.hidden = NO;
        self.activityTableView.hidden = NO;
        self.selectedIndexs = [NSMutableArray array];
    }
    return self;
}

+ (instancetype)createSelecteActivityViewWithFrame:(CGRect)frame{
    return [[ICMAppointmentActivityView alloc]initSelecteActivityViewWithFrame:frame];
}

#pragma mark    tableViewDelegate tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.activityModels.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICMAppointmentActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICMSelecteActivityCell"];
    if (!cell) {
        cell = [[ICMAppointmentActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ICMSelecteActivityCell"];
    }
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setupSelecteActivityCellWithModel:self.activityModels[indexPath.row] andSelected:[self.selectedIndexs containsObject:[@(indexPath.row)stringValue]]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (self.isPayForType) {
//        [self showString:@"索赔维修类型不能参加活动!" forSeconds:1.5];
//        return;
//    }
    ICMAppointmentActivityCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.stateButton.selected = !cell.stateButton.selected;
    
    ICMSelecteActivityModel *model = self.activityModels[indexPath.row];
    [self.selectedActivityCodes containsObject:model.subTitle]?[self.selectedActivityCodes removeObject:model.subTitle]:[self.selectedActivityCodes PACA_AddObject:model.subTitle];
    
    [self.selectedIndexs containsObject:[@(indexPath.row) stringValue]]?[self.selectedIndexs removeObject:[@(indexPath.row) stringValue]]:[self.selectedIndexs PACA_AddObject:[@(indexPath.row) stringValue]];
}
- (void)didSelectedCell:(ICMAppointmentActivityCell *)cell{
    NSIndexPath *indexPath = [self.activityTableView indexPathForCell:cell];
    [self tableView:self.activityTableView didSelectRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(100);
}

#pragma mark    获取活动数据
//参数 当前时间
//参数 VIN码
- (void)getActivityWithData:(id)data{
    if ([data[@"CODE"] integerValue] == 200) {
        NSArray *dataArray = data[@"TT_ACTIVITY"];
        [self.activityModels removeAllObjects];
        [self.selectedIndexs removeAllObjects];
        if (dataArray.count != 0 && dataArray) {
            for (NSDictionary *activityData in dataArray) {
                ICMSelecteActivityModel *model = [[ICMSelecteActivityModel alloc]init];
                if(!activityData[@"ACTIVITY_NAME"] || activityData[@"ACTIVITY_NAME"] == [NSNull null])
                {
                    model.title = @"";
                }
                else
                {
                    model.title = activityData[@"ACTIVITY_NAME"];
                }
                model.type = [activityData[@"ACTIVITY_TYPE"] isKindOfClass:[NSString class]]?activityData[@"ACTIVITY_TYPE"]:[activityData[@"ACTIVITY_TYPE"] stringValue];
                if(!activityData[@"ACTIVITY_CODE"] || activityData[@"ACTIVITY_CODE"] == [NSNull null])
                {
                    model.subTitle = @"";
                }
                else
                {
                    model.subTitle = activityData[@"ACTIVITY_CODE"];
                }
                if (activityData[@"SALES_DATE_BEGIN"]) {
                    model.startTime = [ICrEAMTool getDateWithTimeInterval:activityData[@"SALES_DATE_BEGIN"]];
                }
                if (activityData[@"SALES_DATE_END"]) {
                    model.endTime = [ICrEAMTool getDateWithTimeInterval:activityData[@"SALES_DATE_END"]];
                }
                if ([model.type isEqualToString:ICMEnumIntToString(ICMEnumActivityTypeDefault)]) {
                    [self.activityModels insertObject:model atIndex:0];
                }else{
                    [self.activityModels PACA_AddObject:model];
                }
            }
            [self.activityTableView reloadData];
            if (self.isEditType) {
                ICMActivityModel *activityModel1 = [[ICMActivityModel alloc]init];
                activityModel1.TT_RECPTION_ACTIVITY = [NSMutableArray array];
                if (self.basicOtherModel.JOIN_ACTIVITY_CODE && ![self.basicOtherModel.JOIN_ACTIVITY_CODE isEqualToString:@""]) {
                    NSArray *activityCodes = [self.basicOtherModel.JOIN_ACTIVITY_CODE componentsSeparatedByString:@";"];
                    for (NSString *code in activityCodes) {
                        ICMActivityItemModel *activityModel = [[ICMActivityItemModel alloc]init];
                        activityModel.ACTIVITY_CODE = code;
                        [activityModel1.TT_RECPTION_ACTIVITY PACA_AddObject:activityModel];
                    }
                }
                [self setupSelecteActivityViewWithActivityModel:activityModel1];
            }else if (self.selectedActivityCodes.count){
                ICMActivityModel *activityModel1 = [[ICMActivityModel alloc]init];
                activityModel1.TT_RECPTION_ACTIVITY = [NSMutableArray array];
                for (NSString *code in self.selectedActivityCodes) {
                    ICMActivityItemModel *activityModel = [[ICMActivityItemModel alloc]init];
                    activityModel.ACTIVITY_CODE = code;
                    [activityModel1.TT_RECPTION_ACTIVITY PACA_AddObject:activityModel];
                }
                [self.selectedActivityCodes removeAllObjects];
                [self setupSelecteActivityViewWithActivityModel:activityModel1];
            }
        }else{
            //[self showString:@"无可参加的服务活动！" forSeconds:1.5];
            [self.activityTableView reloadData];
        }
    }
    else
    {
        //[self showString:@"无可参加的服务活动！" forSeconds:1.5];
    }
}

//显示信息
- (void)showActMsg
{
    if(!self.activityModels || self.activityModels.count == 0)
    {
        [self showString:@"无可参加的服务活动！" forSeconds:1.5];
    }
}

//getter
- (NSString *)carType{
    return self.carTypeView.noStr?:@"";
}
- (NSString *)stepInTime{
    _stepInTime = [[self.stepInTimeView.contentText?:@"" componentsSeparatedByString:@" "]firstObject];
    return _stepInTime;
}


- (NSMutableArray *)selectedActivitys{
    _selectedActivitys = [NSMutableArray array];
    for (NSString *index in self.selectedIndexs) {
        ICMSelecteActivityModel *model = self.activityModels[[index integerValue]];
        ICMActivityItemModel *activityModel = [[ICMActivityItemModel alloc]init];
        activityModel.ACTIVITY_CODE = model.subTitle;
        activityModel.ACTIVITY_NAME = model.title;
        [_selectedActivitys PACA_AddObject:activityModel];
    }
    return _selectedActivitys;
}

- (UITableView *)activityTableView{
    if (!_activityTableView) {
        _activityTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _activityTableView.delegate = self;
        _activityTableView.dataSource = self;
        _activityTableView.backgroundColor = BackgroundColor_White;
        [_activityTableView registerClass:[ICMAppointmentActivityCell class] forCellReuseIdentifier:@"ICMSelecteActivityCell"];
        _activityTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _activityTableView.tableFooterView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_activityTableView];
    }
    return _activityTableView;
}

- (NSMutableArray *)activityModels{
    if (!_activityModels) {
        _activityModels = [NSMutableArray array];
    }
    return _activityModels;
}
#pragma mark    根据模型设置界面
- (void)setupSelecteActivityViewWithActivityModel:(ICMActivityModel *)activityModel{
    self.selectedIndexs = [NSMutableArray array];
    for (NSInteger i=0; i<self.activityModels.count; i++) {
        ICMSelecteActivityModel *itemModel = self.activityModels[i];
        for (ICMActivityItemModel *selectedModel in activityModel.TT_RECPTION_ACTIVITY) {
            if ([itemModel.subTitle isEqualToString:selectedModel.ACTIVITY_CODE]) {
                [self.selectedIndexs PACA_AddObject:[@(i) stringValue]];
            }
        }
    }
    dispatch_main_async_safe(^{
        [self.activityTableView reloadData];
    });
}

- (void)setIsPayForType:(BOOL)isPayForType{
    _isPayForType = isPayForType;
    if (isPayForType) {
        [self.selectedIndexs removeAllObjects];
        [self.selectedActivityCodes removeAllObjects];
        [self.activityTableView reloadData];
    }
}

- (NSMutableArray *)selectedActivityCodes{
    if (!_selectedActivityCodes) {
        _selectedActivityCodes = [NSMutableArray array];
    }
    return _selectedActivityCodes;
}


@end


#pragma mark    ICMSelecteActivityCell
@implementation ICMAppointmentActivityCell
#pragma mark    设置界面
- (void)setupSelecteActivityCellWithModel:(ICMSelecteActivityModel *)model andSelected:(BOOL)isSelected{
    self.contentView.backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.00f];
    self.activityNameLabel.text = model.title;
    self.activityNameLabel.textColor = [model.type isEqualToString:ICMEnumIntToString(ICMEnumActivityTypeDefault)]?[UIColor redColor]:[UIColor blackColor];
    self.subNameLabel.text = model.subTitle;
    self.startTimeLabel.text = model.startTime;
    self.endTimeLabel.text = model.endTime;
    self.stateButton.selected = isSelected;
    
}

- (void)stateButttonAction:(UIButton*)sender{
    if ([self.delegate respondsToSelector:@selector(didSelectedCell:)]) {
        [self.delegate didSelectedCell:self];
    }
}

/****init******/

- (UIButton *)stateButton{
    if (!_stateButton) {
        _stateButton = [[UIButton alloc]initWithFrame:CGRectMake(ActualWidth(56), ActualHeight(40), ActualWidth(20), ActualHeight(20))];
        [_stateButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:@"btn_activity_notchoose"] selectedBackImage:[UIImage imageNamed:Appointment_ActivityChoose] actionSEL:@selector(stateButttonAction:) target:self];
        [self.contentView addSubview:_stateButton];
    }
    return _stateButton;
}

- (UILabel *)activityNameLabel{
    if (!_activityNameLabel) {
        _activityNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(125), 0, ActualWidth(675), ActualHeight(50))];
        [_activityNameLabel setUpLabelWithText:@"" alignment:NSTextAlignmentLeft textSize:22 textColor:[UIColor blackColor]];
        [self.contentView addSubview:_activityNameLabel];
    }
    return _activityNameLabel;
}

- (UILabel *)subNameLabel{
    if (!_subNameLabel) {
        _subNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(125), ActualHeight(50), ActualWidth(675), ActualHeight(50))];
        [_subNameLabel setUpLabelWithText:@"" alignment:NSTextAlignmentLeft textSize:14 textColor:[UIColor blackColor]];
        [self.contentView addSubview:_subNameLabel];
    }
    return _subNameLabel;
}

- (UILabel *)startTimeLabel{
    if (!_startTimeLabel) {
        _startTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-ActualWidth(225), ActualHeight(25), ActualWidth(225), ActualHeight(25))];
        [_startTimeLabel setUpLabelWithText:@"" alignment:NSTextAlignmentCenter textSize:14 textColor:[UIColor blackColor]];
        [self.contentView addSubview:_startTimeLabel];
    }
    return _startTimeLabel;
}

- (UILabel *)endTimeLabel{
    if (!_endTimeLabel) {
        _endTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-ActualWidth(225), ActualHeight(50), ActualWidth(225), ActualHeight(25))];
        [_endTimeLabel setUpLabelWithText:@"" alignment:NSTextAlignmentCenter textSize:14 textColor:[UIColor blackColor]];
        [self.contentView addSubview:_endTimeLabel];
    }
    return _endTimeLabel;
}

@end
